package net.wanho.day02;

import net.wanho.dao.StudentDaoI;
import net.wanho.entity.Student;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;
import java.sql.SQLException;
import java.util.Date;
import java.util.List;

public class StudentTest {
    static String config="mybatisConfig.xml";
    static SqlSession ss=null;
    static{
        InputStream in= null;
        try {
            in = Resources.getResourceAsStream(config);
        } catch (IOException e) {
            e.printStackTrace();
        }
        SqlSessionFactory ssf = new SqlSessionFactoryBuilder().build(in);
        ss=ssf.openSession();
    }

    public static void main(String[] args) throws SQLException {
//        selectAllStudents();
        selectStudent("小");
//        insertStudent();
//        deleteStudent("3");
//        updateStudent(new Student());
    }
    public static void selectAllStudents(){
        StudentDaoI studentDao=ss.getMapper(StudentDaoI.class);
        List<Student> list=studentDao.selectAllStudents();
        System.out.println(list);
    }

    public static void selectStudent(String sname){
        StudentDaoI studentDao=ss.getMapper(StudentDaoI.class);
        List<Student> list=studentDao.selectStudent(sname);
        System.out.println(list);
    }

    public static void insertStudent() throws SQLException {
        ss.getConnection().setAutoCommit(true);
        StudentDaoI studentDao=ss.getMapper(StudentDaoI.class);
        Student student=new Student("3","小么","女",new Date(),new Date(),"0");
        studentDao.insertStudent(student);
    }

    public static void deleteStudent(String sid) throws SQLException {
        ss.getConnection().setAutoCommit(true);
        StudentDaoI studentDao=ss.getMapper(StudentDaoI.class);
        studentDao.deleteStudent(sid);
    }

    public static void updateStudent(Student student) throws SQLException {
        ss.getConnection().setAutoCommit(true);
        StudentDaoI studentDao=ss.getMapper(StudentDaoI.class);
        Student student1=new Student("3","大么","女",new Date(),new Date(),"0");
        studentDao.updateStudent(student1);
    }
}
